PROJECT_NAME := newshow
VERSION := 0.1.0

DOCKER_NET_NAME := $(PROJECT_NAME)-net

DOCKER_IMAGE_DB := "$(PROJECT_NAME)/db:$(VERSION)"
DOCKER_IMAGE_REDIS := "$(PROJECT_NAME)/redis:$(VERSION)"
DOCKER_IMAGE_WEB := "$(PROJECT_NAME)/web:$(VERSION)"
DOCKER_IMAGE_IM := "$(PROJECT_NAME)/im:$(VERSION)"

MYSQL_DB_NAME := newshow
MYSQL_USER_NAME := newshow
MYSQL_PASSWORD := newshow
MYSQL_PORT := 3306
MYSQL_OUT_PORT := 3307

build:
	@docker build -t $(DOCKER_IMAGE_DB) docker/db/.
	@docker build -t $(DOCKER_IMAGE_REDIS) docker/redis/.
	@docker build -t $(DOCKER_IMAGE_WEB) docker/web/.
	@docker build -t $(DOCKER_IMAGE_IM) docker/im/.

backup: 
	@mysqldump -h127.0.0.1 -P$(MYSQL_OUT_PORT) \
	-u$(MYSQL_USER_NAME) -p$(MYSQL_PASSWORD) \
	--skip-extended-insert \
	--databases \
	 $(MYSQL_DB_NAME) > ./docker/db/$(MYSQL_DB_NAME).sql

clean:
	@docker image rm -f $(DOCKER_IMAGE_DB)
	@docker image rm -f $(DOCKER_IMAGE_REDIS)
	@docker image rm -f $(DOCKER_IMAGE_WEB)
	@docker image rm -f $(DOCKER_IMAGE_IM)

run-db:
	# 启动 DB 服务
	@docker run --rm -d -p $(MYSQL_OUT_PORT):$(MYSQL_PORT) \
	-e MYSQL_ROOT_PASSWORD=$(MYSQL_PASSWORD) \
	-e MYSQL_DATABASE=$(MYSQL_DB_NAME) \
	-e MYSQL_USER=$(MYSQL_USER_NAME) \
	-e MYSQL_PASSWORD=$(MYSQL_PASSWORD) \
	--name $(PROJECT_NAME)-db \
	--network $(DOCKER_NET_NAME) \
	$(DOCKER_IMAGE_DB) \
	--default_authentication_plugin=mysql_native_password \
	--character-set-server=utf8mb4 \
	--collation-server=utf8mb4_unicode_ci

run-redis:
	# 启动 REDIS 服务
	@docker run --rm -d -p 6380:6379 \
	--name $(PROJECT_NAME)-redis \
	--network $(DOCKER_NET_NAME) \
	$(DOCKER_IMAGE_REDIS)

run-web:
	# 启动 WEB 服务
	@docker run --rm -d -p 8082:80 \
	-v `pwd`:/var/www/html \
	--name $(PROJECT_NAME)-web \
	--network $(DOCKER_NET_NAME) \
	$(DOCKER_IMAGE_WEB)

run-im:
	# 启动 IM 服务
	@docker run --rm -d -p 19967:19967 \
	-v `pwd`/LiveNewIM:/var/script \
	--name $(PROJECT_NAME)-im \
	--network $(DOCKER_NET_NAME) \
	$(DOCKER_IMAGE_IM)

run:
	# 启动 Docker 网络
	@docker network create -d bridge $(DOCKER_NET_NAME)
	@make run-db
	@make run-redis
	@make run-web
	@make run-im
	@echo "服务启动完成"

stop:
	# 备份数据库
	@make backup
	# 关闭 DB 服务
	@docker stop $(PROJECT_NAME)-db
	# 关闭 REDIS 服务
	@docker stop $(PROJECT_NAME)-redis
	# 关闭 WEB 服务
	@docker stop $(PROJECT_NAME)-web
	# 关闭 IM 服务
	@docker stop $(PROJECT_NAME)-im
	# 关闭 Docker 网络
	@docker network rm $(DOCKER_NET_NAME)
	@echo "服务已停止"



